// vi:set ft=javascript ff=dos ts=4 sts=4 sw=4 et:

// ==PREPROCESSOR==
// @name "Drag Drop Basic"
// @author "T.P Wang"
// Add feature below to enable drag and drop support
// @feature "dragdrop"
// ==/PREPROCESSOR==

function RGB(r, g, b) {
    return (0xff000000 | (r << 16) | (g << 8) | (b));
}

var color_background    =   RGB(255, 255, 255);
var color_background2   =   RGB(193, 219, 252);
var dragging            =   false;

function on_paint(gr) {
    var color = dragging ? color_background2 : color_background;

    gr.FillSolidRect(0, 0, window.Width, window.Height, color);
}

function on_drag_enter() {
    dragging = true;
    window.Repaint();
}

function on_drag_leave() {
    dragging = false;
    window.Repaint();
}

// Here we create a new playlist for hosting the dropped objects.
function on_drag_drop(action) {
    var playlist_name = "Dropped Items";
    var idx = -1;

    // Find the playlist first.
    for (var i = 0; i < fb.PlaylistCount; ++i) {
        if (fb.GetPlaylistName(i) == playlist_name) {
            idx = i;
            break;
        }
    }

    // If not found, then create one.
    if (idx == -1)
        idx = fb.CreatePlaylist(fb.PlaylistCount, playlist_name);
    
    // This is required because when a drop event is fired, you won't get on_drag_leave() called.
    dragging = false;
    window.Repaint();
    
    // We are going to process the dropped items to a playlist.
    action.ToPlaylist();
    action.Playlist = idx;
    action.ToSelect = false;

    // Dropped items will be added to the active playlist later.
}
